package com.gr.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: AutoConfiguration
 * Description:
 *
 * @author binbin_hao
 * @date 2023/9/15 17:24
 */
@Configuration
@EnableConfigurationProperties(MqttConfig.class)
@ConditionalOnClass(MqttConfig.class)
@ConditionalOnProperty(prefix = "mqtt", value = "enabled", matchIfMissing = true)
public class AutoConfiguration {
    @Resource
    private MqttClientManager mqttClientManager;
    @Autowired
    private MqttConfig mqttConfig;
    private static final Logger logger = LoggerFactory.getLogger(AutoConfiguration.class);
    /**
     * 这里面在springboot 启动后加载装配
     */
    @PostConstruct
    public  void createMqttClient(){
        List<MqttClientProperties> mqttClientPropertiesList= mqttConfig.getClientList();
        if (!CollectionUtils.isEmpty(mqttClientPropertiesList)){
            for (MqttClientProperties properties : mqttClientPropertiesList){
                mqttClientManager.createMqttClient(properties);
            }
        }else {
            logger.warn("没有得到对应MQTT 配置实体类");
        }

    }

}
